﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.Converters
{
    public class CheckAllConverter : IMultiValueConverter
    {
        private object[] convertValues = null;

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            convertValues = new object[values.Length];

            for (int i = 0; i < values.Length; i++)
            {
                convertValues[i] = values[i];
            }

            for (int i = 0; i < values.Length; i += 2)
            {
                bool isChecked = (bool)values[i];
                bool isEnabled = (bool)values[i + 1];

                if (isEnabled == false)
                {
                    continue;
                }

                if (isChecked == false)
                {
                    return false;
                }
            }
            return true;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            object[] values = new object[targetTypes.Length];
            for (int i = 0; i < values.Length; i += 2)
            {
                if (convertValues != null && (bool)convertValues[i + 1] == false)
                    values[i] = convertValues[i];
                else
                    values[i] = value;

                values[i + 1] = null;
            }
            return values;
        }
    } 
}
